package dao;

import java.util.ArrayList;
import java.util.List;

import entity.Pet;

public class PetDao extends BaseDao {

	/**
	 * 根据id获取宠物信息
	 * @param petId
	 * @return
	 */
	public Pet getPetByPetId(int petId) {
		try {
			rs = executeQuery("select * from pet where petId = ?", petId);
			if (rs.next()) {
				Pet pet = new Pet();
				pet.setBirthday(rs.getDate("birthday"));
				pet.setDesc(rs.getString("desc"));
				pet.setPetBreed(rs.getString("petBreed"));
				pet.setPetId(rs.getInt("petId"));
				pet.setPetName(rs.getString("petName"));
				pet.setPetSex(rs.getString("petSex"));
				return pet;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 查询所有宠物
	 * 
	 * @param petBreed
	 *            品种
	 * @return
	 */
	public List<Pet> selectAll(String petBreed) {
		try {
			if (petBreed != null && petBreed != "") {
				rs = executeQuery("select * from pet where petBreed = ?", petBreed);
			} else {
				rs = executeQuery("select * from pet ");
			}
			List<Pet> petList = new ArrayList<Pet>();
			while (rs.next()) {
				Pet pet = new Pet();
				pet.setBirthday(rs.getDate("birthday"));
				pet.setDesc(rs.getString("desc"));
				pet.setPetBreed(rs.getString("petBreed"));
				pet.setPetId(rs.getInt("petId"));
				pet.setPetName(rs.getString("petName"));
				pet.setPetSex(rs.getString("petSex"));
				petList.add(pet);
			}
			return petList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据昵称查询宠物
	 * @param petName
	 * @param petId
	 * @param f true 添加判断 
	 * @return
	 */
	public int selectPetByPetName(String petName,int petId,boolean f) {
		if(!f){
			 return executeCount("select count(1) from pet where petName = ?  and petId != ?", petName,petId);
		}else{
			return executeCount("select count(1) from pet where petName = ? ", petName);
		}
	}

	/**
	 * 添加宠物
	 * 
	 * @param pet
	 * @return
	 */
	public int addPet(Pet pet) {
		return executeUpdate("insert into pet values(null,?,?,?,?,?)", pet.getPetName(), pet.getPetBreed(),
				pet.getPetSex(), pet.getBirthday(), pet.getDesc());
	}
	
	public int updatePet(Pet pet) {
		return executeUpdate("update pet set petName=?,petBreed=?,petSex=?,birthday=?,`desc`=? where petId = ?", pet.getPetName(), pet.getPetBreed(),
				pet.getPetSex(), pet.getBirthday(), pet.getDesc(),pet.getPetId());
	}
}
